package com.tarena.lbs.user.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.service.AdminService;
import com.tarena.lbs.user.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理和后台用户相关的业务接口
 */

@Slf4j
@RestController
@Tag(name = "后台用户接口", description = "处理和后台用户相关的业务接口")
public class AdminController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private AdminService adminService;

    @Autowired
    JwtEncoder<UserPrinciple> jwtEncoder;


    @Operation(summary = "管理员登录")
    @PostMapping("/passport/admin/login")
    public Result<LoginVO> adminLogin(@Validated @RequestBody AdminLoginParam param) throws BusinessException {
        log.debug("管理员登录业务处理,入参:{}",param);
        if (param.getPhone() == null || param.getPhone().trim().isEmpty()) {
            throw new BusinessException( "-1", "手机号不能为空");
        }
        LoginVO vo = loginService.doLogin(param);
        log.debug("管理员登录成功，入参:{},出参:{}",param,vo);
        return new Result<>(vo);
    }


    @Operation(summary = "用户详情")
    @GetMapping("/admin/basic/role/detail")
    public Result<AdminVO> detail(@RequestHeader("Authorization") String token) throws BusinessException {
        log.debug("查询登录用户详情");
        if (StringUtils.isBlank(token)){
            throw new BusinessException("-2","token不能为空");
        }
         try{
             UserPrinciple userPrinciple = jwtEncoder.getLoginFromToken(token, UserPrinciple.class);
             if (userPrinciple == null){
                 throw new BusinessException("-2","token错误");
             }
             AdminVO vo = adminService.detail();
             log.debug("查询登录用户详情成功，出参:{}",vo);
             return new Result<>(vo);
         }catch (BusinessException e){
             log.error("查询登录用户详情失败，出参:{}",e);
             throw e;
         }
    }


    /**
     * 查询后台管理的 账号列表
     */
    @Operation(summary = "账号列表")
    @PostMapping("/admin/basic/role/list")
    public Result<List<AdminPO>> pageList(@RequestBody AdminQuery query)
        throws BusinessException{
        List<AdminPO> list = adminService.pageList(query);
        System.out.println("=============================账号列表查询成功，出参:{}"+list);
        return new Result<>(list);
    }

    @Operation(summary = "账号保存")
    @PostMapping("/admin/basic/role/add")
    public Result<Void> save(@RequestBody AdminParam param)
        throws BusinessException{
        log.debug("账号保存,controller层{}",param);
        adminService.save(param);
        return Result.success();
    }

}